import * as echarts from 'echarts'

const getPercent = (num, total) => {
  num = parseFloat(num)
  total = parseFloat(total)
  if (isNaN(num) || isNaN(total)) {
    return '-'
  }
  return total <= 0 ? '0%' : Math.round((num / total) * 10000) / 100.0 + '%'
}

const Radar2Option = (scoreList) => {
  let scoreArr = [],
    indicator = [],
    max_arr = []

  scoreList.map((item) => {
    const { title, score, fullMark } = item
    switch (title) {
      case '创新投入':
        item.key = 'investment'
        indicator.push({ name: '创新投入', max: fullMark })
        break
      case '创新条件':
        item.key = 'condition'
        indicator.push({ name: '创新条件', max: fullMark })
        break
      case '创新绩效':
        item.key = 'merits'
        indicator.push({ name: '创新绩效', max: fullMark })
        break
      case '加分项':
        item.key = 'bonus'
        indicator.push({ name: '加分项', max: fullMark })
        break
      default:
        break
    }
    scoreArr.push(score)
    max_arr.push(fullMark)
    return item
  })

  return {
    radar: {
      name: {
        textStyle: {
          color: '#333',
          fontSize: 18,
          borderRadius: 3,
          padding: [3, 5],
        },
      },
      indicator,
    },
    series: [
      {
        type: 'radar',
        data: [{ value: scoreArr }],
        areaStyle: {
          color: 'rgba(2, 99, 255, .8)',
        },
        label: {
          show: true,
          color: 'rgba(51, 51, 51, 1)',
          fontSize: 18,
          formatter: function (params) {
            let val = getPercent(params.value, max_arr[params.dimensionIndex])
            return val
          },
        },
        itemStyle: {
          color: '#fff',
          borderColor: '#0263FF',
          borderWidth: 2,
        },
        lineStyle: {
          color: '#0263FF',
        },
      },
    ],
  }
}

export { Radar2Option }
